//
//  PTSheetHandle.swift
//  Donpo
//
//  Created by benwang on 2020/1/14.
//  Copyright © 2020 benco. All rights reserved.
//

/**
 * 通用性，可将自定义视图方便的嵌入
 * 1.通过扩展PTSheetHandle增加自动定义方法
 * 2.通过扩展PTSheetHandle.Content增加自定义视图
 *
 */

import UIKit
import SnapKit

class PTSheetHandle: NSObject {
    struct Content {
        let sheet: PTSheetConfigurable&UIView
    }

    typealias Index = Int
    typealias Handle = (Index) -> Void

    private var window: UIWindow? {
      if let window = UIApplication.shared.delegate?.window as? UIWindow {
        return window
      }
      return UIApplication.shared.keyWindow
    }

    private var backgroundView: UIView = {
        return UIView(frame: UIScreen.main.bounds)
    }()

    private var sheet: (PTSheetConfigurable&UIView)?
    private var closable = true

    func sheet(_ content: Content, handle: Handle? = nil) {
        self.closable = false
        self.sheet = content.sheet
        guard let window = self.window else { return }
        guard var sheet = self.sheet else { return }
        window.endEditing(true)

        let handler: (Index) -> Void = { [weak self] index in
            guard let self = self else { return }
            self.closable = true
            self.sheet?.fadeOut()
            handle?(index)
            self.closeWindow()
        }

        if !sheet.isDescendant(of: backgroundView) {
            backgroundView.addSubview(sheet)
            sheet.handle = handler
            sheet.fadeIn()
            sheet.shouldUpdateLayoutConstraints()
        }

        if !backgroundView.isDescendant(of: window) {
            window.addSubview(backgroundView)

            backgroundView.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
            backgroundView.fadeIn()
            backgroundView.backgroundColor = UIColor(white: 0.2, alpha: 0.5)
        }
    }

    func closeWindow() {
        guard self.closable else { return }

        UIView.animate(withDuration: 0.2, animations: {
            self.backgroundView.alpha = 0
        }, completion: { (completed) in
            guard completed else { return }
            self.backgroundView.removeFromSuperview()
            self.sheet?.removeFromSuperview()
        })
    }
}

private extension UIView {
    func fadeIn(transform: Bool = false) {
        alpha = 0
        UIView.animate(withDuration: 0.2) {
            self.alpha = 1
            guard transform else { return }
            self.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
        }
    }
    func fadeOut() {
        UIView.animate(withDuration: 0.2, animations: {
            self.alpha = 0
        }, completion: { (completed) in
            guard completed else { return }
            self.removeFromSuperview()
        })
    }
}

/// 响应索引值
extension PTSheetHandle.Index {
    static let cancel = 1000723
    static let ensure = 1000927
}

/// 可配置的弹框协议
protocol PTSheetConfigurable {

    var handle: PTSheetHandle.Handle? { get set}

    func shouldUpdateLayoutConstraints()
}
